package com.softeem.dao.impl;

import com.softeem.bean.CartItem;
import com.softeem.dao.CartItemDao;
import com.softeem.utils.BaseDao;

import java.sql.SQLException;
import java.util.List;

public class CartItemDaoImpl extends BaseDao<CartItem> implements CartItemDao {
    public int addCart(CartItem cartItem) throws SQLException {
        return this.update("insert into cart_item values(null,?,?,?,?,?)",
                cartItem.getName(), cartItem.getCount(), cartItem.getPrice(),
                cartItem.getTotalPrice(), cartItem.getUid()
        );
    }

    @Override
    public int deleteCartById(Integer id)throws SQLException {

        return this.update("delete from cart_item where id=?", id);
    }

    @Override
    public int deleteCartByUid(Integer uid)throws SQLException {
        return this.update("delete from cart_item where uid=?", uid);
    }

    @Override
    public int updateCartCountById(Integer id, Integer count, Double totalPrice) throws SQLException{
        return this.update("update cart_item set count = ?,total_price=? where id = ?", count, totalPrice, id);
    }

    @Override
    public CartItem findByNameAndUid(String name, Integer uid) throws SQLException{
        return this.getBean("select * from cart_item where name=? and uid=?", name, uid);
    }

    @Override
    public Integer findByUidCount(Integer uid) throws SQLException{
        return this.getValue("select count(*) from cart_item where uid=?", uid).intValue();
    }

    @Override
    public List<CartItem> findCartByUid(int begin, int pageSize, Integer uid) throws SQLException{
        return this.getBeanList("select * from cart_item where uid=? order by id desc limit ?,?", uid , begin , pageSize);
    }

    @Override
    public CartItem findById(Integer id) throws SQLException{
        return super.getBean("select * from cart_item where id = ?" , id);
    }

}
